<?php

/**
 * The admin-specific functionality of the plugin.
 *
 * @link       https://git.oschina.net/wostech/wp-wxtpp
 * @since      1.0.0
 *
 * @package    WP_WXTPP
 * @subpackage WP_WXTPP/admin
 */

/**
 * The admin-specific functionality of the plugin.
 *
 * Defines the plugin name, version, and two examples hooks for how to
 * enqueue the admin-specific stylesheet and JavaScript.
 *
 * @package    WP_WXTPP
 * @subpackage WP_WXTPP/admin
 * @author     Yajun Hu <yjhu@126.com>
 */
class WP_WXTPP_Admin {

	/**
	 * The ID of this plugin.
	 *
	 * @since    1.0.0
	 * @access   private
	 * @var      string    $plugin_name    The ID of this plugin.
	 */
	private $plugin_name;

	/**
	 * The version of this plugin.
	 *
	 * @since    1.0.0
	 * @access   private
	 * @var      string    $version    The current version of this plugin.
	 */
	private $version;

	/**
	 * Initialize the class and set its properties.
	 *
	 * @since    1.0.0
	 * @param      string    $plugin_name       The name of this plugin.
	 * @param      string    $version    The version of this plugin.
	 */
	public function __construct( $plugin_name, $version ) {

		$this->plugin_name = $plugin_name;
		$this->version = $version;

	}
        
        public function display_settings_page() {
            ?>
            <div class="wrap">
		<h2><?php _e( 'WXTPP settings', 'wp-wxtpp' ); ?></h2>
                <form action="<?php echo is_multisite() ? '../options.php' : 'options.php' ?>" method="post">
                    <?php settings_fields('wxtpp_settings'); ?>
                    <?php do_settings_sections('wxtpp-settings'); ?> 
                    <input name="Submit" type="submit" class="button button-primary" value="<?php esc_attr_e('Save Changes'); ?>" />
                </form>
            </div>
            <?php
        }
        
        public function display_login_page() {
            require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/class-wxtpp.php'; 
            require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/class-wxtpp-gh.php'; 
            $wxtpp = WXTPP::get_instance();
            $auth_code = $_GET[ 'auth_code' ];
            if ( isset( $auth_code ) ) {
                $wxtpp->clear_pre_auth_code();
                $auth_info = $wxtpp->api_component_query_auth( $auth_code );
                if ( FALSE !== $auth_info ) {
                    $wxtpp_gh = new WXTPP_GH( $auth_info[ 'authorization_info' ][ 'authorizer_appid' ] );
                    $wxtpp_gh->set_access_token( $auth_info[ 'authorization_info' ][ 'authorizer_access_token' ], time() + $auth_info[ 'authorization_info' ][ 'expires_in' ] - 200 );
                    $wxtpp_gh->set_refresh_token( $auth_info[ 'authorization_info' ][ 'authorizer_refresh_token' ] );
                    $wxtpp_gh->set_authorized( TRUE );
                    
                    $auth_details = $wxtpp->api_component_get_authorizer_info( $wxtpp_gh->get_appid() );
                }
                ?>
                <div class="wrap">
                    <h2><?php _e( 'WXTPP login', 'wp-wxtpp' ); ?></h2>
                    <p><?php print_r( $auth_info ); ?></p>
                    <p><?php print_r( $auth_details ); ?></p>
                </div>
                <?php
            } else {            
                $pre_auth_code = $wxtpp->get_pre_auth_code();
                $current_url = urlencode( home_url( add_query_arg( array() ) ) );
                $component_appid = $wxtpp->get_component_appid();
                $component_login_url = "https://mp.weixin.qq.com/cgi-bin/componentloginpage?component_appid={$component_appid}&pre_auth_code={$pre_auth_code}&redirect_uri={$current_url}";
                ?>
                <div class="wrap">
                    <h2><?php _e( 'WXTPP login', 'wp-wxtpp' ); ?></h2>
                    <p><a href="<?php echo $component_login_url; ?>" class="button button-primary"><?php _e( 'WXTPP login', 'wp-wxtpp' ); ?></a></p>
                </div>
                <?php
            }
        }
        
        public function add_menus() {
            add_submenu_page( 'settings.php', __( 'WXTPP settings', 'wp-wxtpp' ), __( 'WXTPP settings', 'wp-wxtpp' ), is_multisite() ? 'manage_network_options' : 'manage_options', 'wxtpp-settings', array( $this, 'display_settings_page' ) ) ;
//            add_submenu_page( 'settings.php', __( 'WXTPP login', 'wp-wxtpp' ), __( 'WXTPP login', 'wp-wxtpp' ), is_multisite() ? 'manage_network_options' : 'manage_options', 'wxtpp-login', array( $this, 'display_login_page' ) ) ;
        }
        
        public function setting_field_callback(array $args) {
            $wxtpp_settings = get_option( 'wxtpp_settings' );
            echo "<input size='60' id='{$args['id']}' name='{$args['name']}' type='{$args['type']}' value='{$wxtpp_settings[$args['key']]}' />";
        }
        
        public function admin_init() {
            register_setting( 'wxtpp_settings', 'wxtpp_settings' );
            add_settings_section( 'default', '', '__return_empty_string', 'wxtpp-settings' );
            add_settings_field( 'wxtpp_appId', __( 'appId', 'wp-wxtpp' ), array( $this, 'setting_field_callback' ),'wxtpp-settings', 'default', array( 'id' => 'wxtpp_appId', 'name' => 'wxtpp_settings[appId]', 'type' => 'text', 'key' => 'appId') );
            add_settings_field( 'wxtpp_appSecret', __( 'appSecret', 'wp-wxtpp' ), array( $this, 'setting_field_callback' ), 'wxtpp-settings', 'default', array( 'id' => 'wxtpp_appSecret', 'name' => 'wxtpp_settings[appSecret]', 'type' => 'text', 'key' => 'appSecret') );
            add_settings_field( 'wxtpp_token', __( 'token', 'wp-wxtpp' ), array( $this, 'setting_field_callback' ), 'wxtpp-settings', 'default', array( 'id' => 'wxtpp_token', 'name' => 'wxtpp_settings[token]', 'type' => 'text', 'key' => 'token') );
            add_settings_field( 'wxtpp_encodingAesKey', __( 'encodingAesKey', 'wp-wxtpp' ), array( $this, 'setting_field_callback' ), 'wxtpp-settings', 'default', array( 'id' => 'wxtpp_encodingAesKey', 'name' => 'wxtpp_settings[encodingAesKey]', 'type' => 'text', 'key' => 'encodingAesKey') );
        }

	/**
	 * Register the stylesheets for the admin area.
	 *
	 * @since    1.0.0
	 */
	public function enqueue_styles() {

		/**
		 * This function is provided for demonstration purposes only.
		 *
		 * An instance of this class should be passed to the run() function
		 * defined in WP_WXTPP_Loader as all of the hooks are defined
		 * in that particular class.
		 *
		 * The WP_WXTPP_Loader will then create the relationship
		 * between the defined hooks and the functions defined in this
		 * class.
		 */                
                wp_enqueue_style( $this->plugin_name . 'font-awesome', '//cdn.bootcss.com/font-awesome/4.7.0/css/font-awesome.min.css' );
		wp_enqueue_style( $this->plugin_name . 'admin-css', plugin_dir_url( __FILE__ ) . 'css/wp-wxtpp-admin.css', array(), $this->version, 'all' );
                

	}

	/**
	 * Register the JavaScript for the admin area.
	 *
	 * @since    1.0.0
	 */
	public function enqueue_scripts() {

		/**
		 * This function is provided for demonstration purposes only.
		 *
		 * An instance of this class should be passed to the run() function
		 * defined in WP_WXTPP_Loader as all of the hooks are defined
		 * in that particular class.
		 *
		 * The WP_WXTPP_Loader will then create the relationship
		 * between the defined hooks and the functions defined in this
		 * class.
		 */

		wp_enqueue_script( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'js/wp-wxtpp-admin.js', array( 'jquery' ), $this->version, false );

	}

}
